import router from "@src/router";
import {
  getAppointmentShopDetail,
  getCategoryList,
  getGoodsList,
} from "@src/api/reservation";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import reservation from "@src/store/reservation";
import { parseTime } from "@src/common/reservation/config/index.js";
import base from "@src/store/base";
Page({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: base,
      fields: ["themeColor", "themeTitleColor", "perms", "businessName"],
    },
    {
      store: reservation,
      fields: ["goodsInfo"],
      actions: ["setSubmitGoodsInfo"],
    },
  ],
  data: {
    resourcesIndex: 0,
    shopInfo: {},
    categoryList: [],
    loading: false,
  },
  onLoad: function (options) {
    this.setData({ loading: true });
    console.log("选择资源", options);
    this.setData({
      options,
    });
    this.fetchData();
  },
  onPullDownRefresh() {
    this.setData({ loading: true });
    this.fetchData();
  },
  async fetchData() {
    const { shopId, categoryIds, planId, date: optionDate } = this.data.options;
    const categoryIdArr = decodeURIComponent(
      decodeURIComponent(categoryIds)
    ).split(",");
    const date = decodeURIComponent(decodeURIComponent(optionDate));
    const { code, data } = await getCategoryList({
      shopId: Number(shopId),
      planId: Number(planId),
      categoryIds: categoryIdArr,
      date,
    });
    if (code === 0) {
      this.setData({ loading: false });
      wx.stopPullDownRefresh();
      data.map((v) => {
        if (v.label) {
          const labels = v.label.split(";");
          v.tags = [];
          labels.map((vc) => {
            if (vc) {
              v.tags.push(vc);
            }
          });
        }
        if (v.appointmentTime) {
          const nowDate = parseTime();
          let time = `${new Date().getFullYear()}年${v.appointmentTime}`
            .replace("年", "/")
            .replace("月", "/")
            .replace("日", "/");
          const newTime = parseTime(time);
          // console.log("时间", time, v.appointmentTime, newTime);
          if (
            nowDate.getDate === newTime.getDate &&
            nowDate.getMonth === newTime.getMonth
          ) {
            v.appointmentTime = `${newTime.getHours}:${newTime.getMinutes}`;
          }
        }
      });
      this.setData({
        categoryList: data,
      });
    }
    const { code: code1, data: data1 } = await getAppointmentShopDetail({
      shopId,
    });
    if (code1 === 0) {
      data1.shopLabel = data1.shopLabel.split(",");
      this.setData({
        shopInfo: data1,
      });
    }
  },
  handleChoose(e) {
    const {
      currentTarget: {
        dataset: { modelvalue },
      },
    } = e;
    this.setData({
      resourcesIndex: modelvalue,
    });
    const choseCategoryInfo = this.data.categoryList[modelvalue];
    const goodsInfo = JSON.parse(
      JSON.stringify(JSON.parse(this.data.goodsInfo))
    );
    const categoryInfo = goodsInfo.categoryInfo;
    const skuInfo = goodsInfo.skuInfo;
    goodsInfo.categoryInfo = {
      ...categoryInfo,
      ...choseCategoryInfo,
      price: categoryInfo.price,
    };
    // goodsInfo.skuInfo = {
    //   ...skuInfo,
    //   ...choseCategoryInfo,
    // };

    this.setSubmitGoodsInfo(JSON.stringify(goodsInfo)).then(() => {
      wx.showLoading({
        title: "保存信息中",
      });
      setTimeout(() => {
        wx.hideLoading();
        console.log("保存信息中", goodsInfo);
        router.push({
          name: "reservation/submit_reservation",
          params: {
            title: 0,
            shopId: this.data.shopInfo.shopInfoResp.shopId,
            // navIndex: e.currentTarget.dataset.index,
          },
        });
      }, 500);
    });
  },
  handleSelect() {
    // this.handleShowSelectTime();
    router.push({
      name: "reservation/submit_reservation",
      params: {
        title: 0,
        shopId: this.data.shopInfo.shopInfoResp.shopId,
        // navIndex: e.currentTarget.dataset.index,
      },
    });
  },
});
